package cn.glutnn.controller;


import cn.glutnn.entity.FoodReviews;
import cn.glutnn.entity.Result;
import cn.glutnn.entity.User;
import cn.glutnn.service.FoodReviewsService;
import cn.glutnn.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (FoodReviews)表控制层
 *
 * @author msh
 * @since 2024-12-14 17:00:37
 */
@RestController
@RequestMapping("/foodReviews")
public class FoodReviewsController {
    /**
     * 服务对象
     */
    @Autowired
    private FoodReviewsService foodReviewsService;
    @Autowired
    private UserService userService;


    @PostMapping("/rate")
    public Result rate(@RequestBody FoodReviews foodReviews) {
        try {
            foodReviewsService.rate(foodReviews);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("评价成功");
    }

    @GetMapping("/{id}")
    public Result<FoodReviews> getById(@PathVariable("id") Integer id) {
        FoodReviews foodReviews = foodReviewsService.getById(id);
        return Result.success(foodReviews);
    }


    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable("id") Integer id) {
        foodReviewsService.removeById(id);
        return Result.success("删除食品评价成功");
    }

    @PutMapping
    public Result<String> update(@RequestBody FoodReviews foodReviews) {
        foodReviewsService.updateById(foodReviews);
        return Result.success("修改食品评价成功");
    }

    // 根据用户id获取用户评价

    @GetMapping("/listFoodReviewsByUserId/{userId}")
    public Result<List<FoodReviews>> listFoodReviewsByUserId(@PathVariable("userId") Integer userId) {
        LambdaQueryWrapper<FoodReviews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FoodReviews::getUserId, userId);
        List<FoodReviews> foodReviewsList = foodReviewsService.list(wrapper);
        return Result.success(foodReviewsList);
    }

    // 根据商家id获取评价列表
    @GetMapping("/listFoodReviewsByBusinessId/{businessId}")
    public Result<List<Map<String, Object>>> getFoodReviewsByBusinessId(@PathVariable("businessId") Integer businessId) {
        // 获取评价列表
        LambdaQueryWrapper<FoodReviews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FoodReviews::getBusinessId, businessId);
        List<FoodReviews> foodReviewsList = foodReviewsService.list(wrapper);

        // 转换结果，添加用户信息
        List<Map<String, Object>> result = new ArrayList<>();
        for (FoodReviews review : foodReviewsList) {
            Map<String, Object> reviewMap = new HashMap<>();
            // 复制评价信息
            reviewMap.put("id", review.getId());
            reviewMap.put("rating", review.getRating());
            reviewMap.put("comment", review.getComment());
            reviewMap.put("createdTime", review.getCreatedTime());

            // 获取用户信息
            User user = userService.getById(review.getUserId());
            if (user != null) {
                reviewMap.put("username", user.getUsername());
                reviewMap.put("userAvatar", user.getAvatar());
            } else {
                reviewMap.put("username", "匿名用户");
                reviewMap.put("userAvatar", null);
            }

            result.add(reviewMap);
        }

        return Result.success(result);
    }


}

